package top.codedance.iotp.iot.sdk.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import top.codedance.iotp.iot.sdk.WsContextAdapter;
import top.codedance.iotp.iot.sdk.entity.StompMessage;
import org.java_websocket.framing.BinaryFrame;

import java.nio.ByteBuffer;

public class StompTool {
    private WsContextAdapter stompWsContext;
    public StompTool(WsContextAdapter stompWsContext){
        this.stompWsContext = stompWsContext;
    }

    /**
     * 连接
     */
    public void connect(){
        StringBuilder message0 = new StringBuilder("CONNECT");
        message0.append("\n");
        message0.append("\n");
        message0.append("\u0000");
        try {
            BinaryFrame binaryFrame = new BinaryFrame();
            ByteBuffer wrap = ByteBuffer.wrap(message0.toString().getBytes("US-ASCII"));
            binaryFrame.setPayload(wrap);
            stompWsContext.sendFrame(binaryFrame);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 订阅
     * @param id
     * @param topic
     */
    public void subscribe(String id, String topic){
        StringBuilder message = new StringBuilder("SUBSCRIBE");
        message.append("\n");
        message.append("id:" + id);
        message.append("\n");
        message.append("destination:" + topic);
        message.append("\n");
        message.append("ack:auto");
        message.append("\n");
        message.append("\n");
        message.append("\u0000");
        try {
            BinaryFrame binaryFrame = new BinaryFrame();
            ByteBuffer wrap = ByteBuffer.wrap(message.toString().getBytes("US-ASCII"));
            binaryFrame.setPayload(wrap);
            stompWsContext.sendFrame(binaryFrame);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 取消订阅
     * @param id
     */
    public void unsubscribe(String id){
        StringBuilder message = new StringBuilder("UNSUBSCRIBE");
        message.append("\n");
        message.append("id:" + id);
        message.append("\n");
        message.append("\n");
        message.append("\u0000");
        try {
            BinaryFrame binaryFrame = new BinaryFrame();
            ByteBuffer wrap = ByteBuffer.wrap(message.toString().getBytes("US-ASCII"));
            binaryFrame.setPayload(wrap);
            stompWsContext.sendFrame(binaryFrame);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 发送数据
     * @param body
     */
    public void send(String body){
        StringBuilder message = new StringBuilder("SEND");
        message.append("\n");
        message.append("\n");

        message.append(body);
        message.append("\u0000");
        try {
            BinaryFrame binaryFrame = new BinaryFrame();
            ByteBuffer wrap = ByteBuffer.wrap(message.toString().getBytes("US-ASCII"));
            binaryFrame.setPayload(wrap);
            stompWsContext.sendFrame(binaryFrame);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 应答
     * @param messageId
     */
    public void ack(String messageId){
        StringBuilder message = new StringBuilder("ACK");
        message.append("\n");
        message.append("message-id:" + messageId);
        message.append("\n");
        message.append("\n");
        message.append("\u0000");
        try {
            BinaryFrame binaryFrame = new BinaryFrame();
            ByteBuffer wrap = ByteBuffer.wrap(message.toString().getBytes("US-ASCII"));
            binaryFrame.setPayload(wrap);
            stompWsContext.sendFrame(binaryFrame);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 解析消息主体
     * @param message
     * @return
     */
    public static StompMessage receive(String message){
        StompMessage stompMessage = new StompMessage();

        return stompMessage;
    }

    /**
     * 解析消息
     * @param body
     * @return
     */
    public static JSONObject message(String body){
        JSONObject obj = new JSONObject();
        try {
            body = body.substring(body.indexOf("\n\n") + 1);
            obj = JSON.parseObject(body);
        }catch (Exception e){
            e.printStackTrace();
        }
        return obj;
    }
}
